package demo1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {

    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode createTree(){
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(3);
        TreeNode D = new TreeNode(4);
        TreeNode E = new TreeNode(5);
        TreeNode F = new TreeNode(6);
        A.left = B;
        B.left = C;
        A.right = D;
        D.left = E;
        D.right = F;
        return A;
    }
    // 前序遍历
    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val);
        inOrder(root.right);


    }
    // 后序遍历
    public void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val);

    }
    int count = 0;
    //求叶子节点
    public int leafCouunt(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            count++;
        }
        leafCouunt(root.left);
        leafCouunt(root.right);
        return count;

    }
    public int leafCount2(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return leafCount2(root.left) + leafCount2(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root, int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+ getKLevelNodeCount(root.right,k-1);

    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if(root ==null){
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);

        return (left > right? left : right) + 1;

    }
    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode findL = find(root.left,val);
        //不等于空要接着走，才能一直走左边
        if(findL != null){
            return findL;
        }
        TreeNode findR = find(root.right,val);
        if(findR != null){
            return findR;
        }
        return null;
    }
    //是否是相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q != null  || p != null && q == null){
            return false;
        }
        if(p == null && q == null){
            return true;
        }

        if(p.val != q.val){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right , q.right);

    }
    //  判断是否是子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null){
            return false;
        }
        if(isSameTree(root,subRoot)){
            return true;
        }
        if(isSubtree(root.left,subRoot)){
            return true;
        }
        if(isSubtree(root.right,subRoot)){
            return true;
        }
        return false;
    }

    //对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return isSymmetricChild(root.left,root.right);

    }
    public boolean isSymmetricChild(TreeNode A ,  TreeNode B){
        if(A == null && B != null
                || A != null && B == null){
            return false;
        }
        if(A == null && B == null){
            return true;
        }
        if(A.val != B.val){
            return false;
        }
        return isSymmetricChild(A.left,B.right)
                && isSymmetricChild(A.right,B.left);
    }
    public void leveOrde(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
            TreeNode Top = queue.poll();
            System.out.print(Top.val + " ");
            if(Top.left != null){
                queue.offer(Top.left);
            }
            if(Top.right != null){
                queue.offer(Top.right);
            }
        }


    }
 public List<List<Integer>> leveOrde2(TreeNode root){
        List<List<Integer>> ret = new LinkedList<>();
        if (ret == null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while(size != 0){
                TreeNode Top = queue.poll();
                list.add(Top.val);
                if(Top.left != null){
                    queue.offer(Top.left);
                }
                if(Top.right != null){
                    queue.offer(Top.right);
            }
                size--;
            }
            ret.add(list);
        }
        return  ret;
    }


}
